package com.csf.sr;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.atomic.AtomicInteger;

import com.csf.sr.api.Service;
import com.csf.sr.api.ServiceRegistry;
import com.csf.sr.api.ServiceUrl;

public class ServiceRegistryImpl implements ServiceRegistry {

	private AtomicInteger serviceId = new AtomicInteger(0);

	private Map<ServiceUrl, Service> serviceMap = new ConcurrentHashMap<ServiceUrl, Service>();

	public Map<ServiceUrl, Service> getServiceMap() {
		return serviceMap;
	}

	public void setServiceMap(Map<ServiceUrl, Service> serviceMap) {
		this.serviceMap = serviceMap;
	}

	@Override
	public int register(Service service, Class serviceInterface) {
		ServiceUrl serviceUrl = new ServiceUrl(serviceInterface);
		serviceUrl.setServiceId(serviceId.addAndGet(1));
		serviceMap.put(serviceUrl, service);
		return serviceId.get();
	}

	@Override
	public void unregister(int serviceId) {
		serviceMap.remove(serviceId);
	}

	public void getAllServices() {
		for (Map.Entry<ServiceUrl, Service> entry : serviceMap.entrySet()) {
			System.out.println("Key : " + entry.getKey() + "Value:"
					+ entry.getValue());
		}
	}


}
